package com.zheng.redpacket.server.utils.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zheng.redpacket.mdg.mapper.UserOrderMapper;
import com.zheng.redpacket.mdg.model.UserOrder;
import com.zheng.redpacket.server.serivce.UserOrderService;
import com.zheng.redpacket.server.utils.publisher.OrderPublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
public class OrderConsumer {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderConsumer.class);

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private UserOrderMapper orderMapper;
    @Autowired
    private UserOrderService orderService;

    @RabbitListener(queues = "${mq.consumer.order.queue.name}", containerFactory = "singleListenerContainer")
    public void consumeMsg(@Payload Integer id) {
        try {
            LOGGER.info("监听到消息内容：orderId={}", id);
            UserOrder userOrder = orderMapper.selectByPrimaryKeyAndStatus(id, 0);
            if (userOrder != null) {
                orderService.updateUserOrderRecord(userOrder);
            }
        } catch (Exception e) {
            LOGGER.error("发生异常",e.fillInStackTrace());
        }
    }
}
